﻿<#
.SYNOPSIS
MySQL Connector/C++ 构建脚本 (PowerShell 版本) - 完全保持原批处理逻辑
#>

# 设置错误处理
$ErrorActionPreference = "Stop"

# 创建输出目录
Write-Host "创建输出目录"
if (-not (Test-Path ".\lib")) { New-Item -ItemType Directory -Path ".\lib" | Out-Null }
if (-not (Test-Path ".\lib\x64")) { New-Item -ItemType Directory -Path ".\lib\x64" | Out-Null }
if (-not (Test-Path ".\lib\Win32")) { New-Item -ItemType Directory -Path ".\lib\Win32" | Out-Null }
if (-not (Test-Path ".\lib\x64\Debug")) { New-Item -ItemType Directory -Path ".\lib\x64\Debug" | Out-Null }
if (-not (Test-Path ".\lib\x64\Release")) { New-Item -ItemType Directory -Path ".\lib\x64\Release" | Out-Null }
if (-not (Test-Path ".\lib\Win32\Debug")) { New-Item -ItemType Directory -Path ".\lib\Win32\Debug" | Out-Null }
if (-not (Test-Path ".\lib\Win32\Release")) { New-Item -ItemType Directory -Path ".\lib\Win32\Release" | Out-Null }
if (-not (Test-Path "..\include")) { New-Item -ItemType Directory -Path "..\include" | Out-Null }

# win32 Release
Write-Host "win32 Release"
if (-not (Test-Path "mysql-connector-c++-8.0.33-win32")) {
    New-Item -ItemType Directory -Path "mysql-connector-c++-8.0.33-win32" | Out-Null
}
Set-Location "mysql-connector-c++-8.0.33-win32"
if (-not (Test-Path "..\mysql-connector-c++-8.0.33-win32.zip")) {
    Invoke-WebRequest -Uri "https://cdn.mysql.com/archives/mysql-connector-c++/mysql-connector-c++-8.0.33-win32.zip" -OutFile "..\mysql-connector-c++-8.0.33-win32.zip"
    Expand-Archive -Path "..\mysql-connector-c++-8.0.33-win32.zip" -DestinationPath "."
}
Set-Location "mysql-connector-c++-8.0.33-win32"
robocopy ".\include\jdbc" "..\..\include\jdbc" /E /COPYALL /R:1 /W:1 /NFL /NDL | Out-Null
robocopy ".\include\mysql" "..\..\include\mysql" /E /COPYALL /R:1 /W:1 /NFL /NDL | Out-Null
robocopy ".\include\mysqlx" "..\..\include\mysqlx" /E /COPYALL /R:1 /W:1 /NFL /NDL | Out-Null
Copy-Item -Path ".\lib\vs14\libcrypto.lib" -Destination "..\..\lib\Win32\Release" -Force
Copy-Item -Path ".\lib\vs14\libssl.lib" -Destination "..\..\lib\Win32\Release" -Force
Copy-Item -Path ".\lib\vs14\mysqlcppconn.lib" -Destination "..\..\lib\Win32\Release" -Force
Copy-Item -Path ".\lib\libcrypto-1_1.dll" -Destination "..\..\lib\Win32\Release" -Force
Copy-Item -Path ".\lib\libssl-1_1.dll" -Destination "..\..\lib\Win32\Release" -Force
Copy-Item -Path ".\lib\mysqlcppconn-9-vs14.dll" -Destination "..\..\lib\Win32\Release" -Force
Copy-Item -Path ".\lib\mysqlcppconn8-2-vs14.dll" -Destination "..\..\lib\Win32\Release" -Force
Set-Location "..\.."

# winx64 Release
Write-Host "winx64 Release"
if (-not (Test-Path "mysql-connector-c++-8.0.33-winx64")) {
    New-Item -ItemType Directory -Path "mysql-connector-c++-8.0.33-winx64" | Out-Null
}
Set-Location "mysql-connector-c++-8.0.33-winx64"
if (-not (Test-Path "..\mysql-connector-c++-8.0.33-winx64.zip")) {
    Invoke-WebRequest -Uri "https://cdn.mysql.com/archives/mysql-connector-c++/mysql-connector-c++-8.0.33-winx64.zip" -OutFile "..\mysql-connector-c++-8.0.33-winx64.zip"
    Expand-Archive -Path "..\mysql-connector-c++-8.0.33-winx64.zip" -DestinationPath "."
}
Set-Location "mysql-connector-c++-8.0.33-winx64"
Copy-Item -Path ".\lib64\vs14\libcrypto.lib" -Destination "..\..\lib\x64\Release" -Force
Copy-Item -Path ".\lib64\vs14\libssl.lib" -Destination "..\..\lib\x64\Release" -Force
Copy-Item -Path ".\lib64\vs14\mysqlcppconn.lib" -Destination "..\..\lib\x64\Release" -Force
Copy-Item -Path ".\lib64\libcrypto-1_1-x64.dll" -Destination "..\..\lib\x64\Release" -Force
Copy-Item -Path ".\lib64\libssl-1_1-x64.dll" -Destination "..\..\lib\x64\Release" -Force
Copy-Item -Path ".\lib64\mysqlcppconn-9-vs14.dll" -Destination "..\..\lib\x64\Release" -Force
Copy-Item -Path ".\lib64\mysqlcppconn8-2-vs14.dll" -Destination "..\..\lib\x64\Release" -Force
Set-Location "..\.."

# win32 Debug
Write-Host "win32 Debug"
if (-not (Test-Path "mysql-connector-c++-8.0.33-win32-debug")) {
    New-Item -ItemType Directory -Path "mysql-connector-c++-8.0.33-win32-debug" | Out-Null
}
Set-Location "mysql-connector-c++-8.0.33-win32-debug"
if (-not (Test-Path "..\mysql-connector-c++-8.0.33-win32-debug.zip")) {
    Invoke-WebRequest -Uri "https://cdn.mysql.com/archives/mysql-connector-c++/mysql-connector-c++-8.0.33-win32-debug.zip" -OutFile "..\mysql-connector-c++-8.0.33-win32-debug.zip"
    Expand-Archive -Path "..\mysql-connector-c++-8.0.33-win32-debug.zip" -DestinationPath "."
}
Set-Location "mysql-connector-c++-8.0.33-win32"
Copy-Item -Path ".\lib\vs14\libcrypto.lib" -Destination "..\..\lib\Win32\Debug" -Force
Copy-Item -Path ".\lib\vs14\libssl.lib" -Destination "..\..\lib\Win32\Debug" -Force
Copy-Item -Path ".\lib\vs14\debug\mysqlcppconn.lib" -Destination "..\..\lib\Win32\Debug" -Force
Copy-Item -Path ".\lib\libcrypto-1_1.dll" -Destination "..\..\lib\Win32\Debug" -Force
Copy-Item -Path ".\lib\libssl-1_1.dll" -Destination "..\..\lib\Win32\Debug" -Force
Copy-Item -Path ".\lib\debug\mysqlcppconn-9-vs14.dll" -Destination "..\..\lib\Win32\Debug" -Force
Copy-Item -Path ".\lib\debug\mysqlcppconn8-2-vs14.dll" -Destination "..\..\lib\Win32\Debug" -Force
Set-Location "..\.."

# winx64 Debug
Write-Host "winx64 Debug"
if (-not (Test-Path "mysql-connector-c++-8.0.33-winx64-debug")) {
    New-Item -ItemType Directory -Path "mysql-connector-c++-8.0.33-winx64-debug" | Out-Null
}
Set-Location "mysql-connector-c++-8.0.33-winx64-debug"
if (-not (Test-Path "..\mysql-connector-c++-8.0.33-winx64-debug.zip")) {
    Invoke-WebRequest -Uri "https://cdn.mysql.com/archives/mysql-connector-c++/mysql-connector-c++-8.0.33-winx64-debug.zip" -OutFile "..\mysql-connector-c++-8.0.33-winx64-debug.zip"
    Expand-Archive -Path "..\mysql-connector-c++-8.0.33-winx64-debug.zip" -DestinationPath "."
}
Set-Location "mysql-connector-c++-8.0.33-winx64"
Copy-Item -Path ".\lib64\vs14\libcrypto.lib" -Destination "..\..\lib\x64\Debug" -Force
Copy-Item -Path ".\lib64\vs14\libssl.lib" -Destination "..\..\lib\x64\Debug" -Force
Copy-Item -Path ".\lib64\vs14\debug\mysqlcppconn.lib" -Destination "..\..\lib\x64\Debug" -Force
Copy-Item -Path ".\lib64\libcrypto-1_1-x64.dll" -Destination "..\..\lib\x64\Debug" -Force
Copy-Item -Path ".\lib64\libssl-1_1-x64.dll" -Destination "..\..\lib\x64\Debug" -Force
Copy-Item -Path ".\lib64\debug\mysqlcppconn-9-vs14.dll" -Destination "..\..\lib\x64\Debug" -Force
Copy-Item -Path ".\lib64\debug\mysqlcppconn8-2-vs14.dll" -Destination "..\..\lib\x64\Debug" -Force
Set-Location "..\.."

Write-Host "MySQL Connector/C++ 构建完成!"